/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_FILE_SOURCE_H
#define MK_FILE_SOURCE_H
#include "MkMediaSource.h"
#include <atomic>

class CMkTimer;
class MkApi CMkFileSource : public CMkMediaSource
{
public:
    typedef struct _file_packet_param
    {
        MkVector<std::pair<Uint32, Uint32>>  VecPacket;     //first offset begin second len
        BOOL                                 bKeyFrame;
        MkCodecType                          CodecType;
    }MkFilePacketParam;
public:
    CMkFileSource(const MkMediaSourceType& SourceType, FILE *fp, CMkTimer* pTimer);
    virtual ~CMkFileSource();

    FILE* GetFp() const { return m_pFp; }
    CMkTimer* GetTimer() const { return m_pTimer; }

    virtual Uint32 ScanFile() = 0;
    virtual Uint32 GetNextFrame(MkVector<MkEsPacket>& VecEsPacket) = 0;
    Uint32 StartMediaSource(const MkSourceResultCallback& ResultCb, const MkEsPacketCallback& EsCb = nullptr) override;
    Uint32 StopMediaSource() override;
    Uint32 PlayControl(const MkPlaybackControlType& ControlType, float fSpeed, Uint32 offsetSec) override;

    const MkVector<MkFilePacketParam>& GetVecFilePacketParam() const { return m_VecPacket; }
    void SetVecFilePacketParam(const MkVector<MkFilePacketParam>& VecParam) { m_VecPacket = VecParam; }
protected:
    MkString                                m_FrameGetTimer;
    Uint8*                                  m_pBuf;
    Uint32                                  m_MaxLen;
    Uint32                                  m_nOffset;
    Uint32                                  m_Pts;
    std::atomic<BOOL>                       m_SendFrame;
    void*                                   m_DeleteWaitHandle;

    MkVector<MkFilePacketParam>             m_VecPacket;
    Uint32                                  m_ReadFrameIndex;
    FILE                                    *m_pFp;
    CMkTimer                                *m_pTimer;

    Uint32                                  m_nSampleRate;
};

class MkApi CMkH264FileSource : public CMkFileSource
{
public:
    CMkH264FileSource(FILE *fp, CMkTimer* pTimer);
    ~CMkH264FileSource();
    Uint32 GetNextFrame(MkVector<MkEsPacket>& VecEsPacket) override;
private:
    Uint32 ScanFile() override;

};

class MkApi CMkH265FileSource : public CMkFileSource
{
public:
    CMkH265FileSource(FILE *fp, CMkTimer* pTimer);
    ~CMkH265FileSource();
    Uint32 GetNextFrame(MkVector<MkEsPacket>& VecEsPacket) override;
private:
    Uint32 ScanFile() override;

};

class MkApi CMkAacFileSource : public CMkFileSource
{
public:
    CMkAacFileSource(FILE *fp, CMkTimer* pTimer);
    ~CMkAacFileSource();
    Uint32 GetNextFrame(MkVector<MkEsPacket>& VecEsPacket) override;
private:
    Uint32 ScanFile() override;

};

#endif